package Service;

import Dao.Impl.PetDaoImpl;
import Dao.Impl.PetOwnerImpl;
import Entity.pet;
import Entity.pet_owner;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PetOwnerServiceImpl implements  PetOwnerService {
    PetOwnerImpl petOwner = new PetOwnerImpl();
    public void GetAllPetOwner() throws SQLException {

        List<pet_owner> pet_owners =new ArrayList<pet_owner>();
        pet_owners=petOwner.AllGetPetOwner();
        for(pet_owner p:pet_owners)
        {
            System.out.println("第"+p.getId()+"个宠物主人，名字为"+p.getName());
        }
    }

    @Override
    public int buy() throws SQLException {
        PetDaoImpl petDao = new PetDaoImpl();
        List<pet> pets = petDao.AllPetByowner();
        System.out.println("******************以下为库存宠物****************");
        int flag1=0;
        for(pet p:pets)
        {
            flag1++;
            System.out.println("序列号为:"+flag1+"     我的名字为:"+p.getName()+"    我是:"+p.getTypename()+"      要购买我要花:"+p.getPrice()+"个元宝");
        }

        System.out.println("---------请选择你要购买的宠物，并输入选择项的序号---------");
        Scanner scanner =new Scanner(System.in);
        String s=scanner.nextLine();
        int flag=Integer.parseInt(s);

        return pets.get(flag-1).getId();

    }

    @Override
    public List<pet> sell(int owner_id) throws SQLException {
        PetDaoImpl petDao = new PetDaoImpl();
        List<pet> pets = petDao.AllPetByyouowner(owner_id);
        return pets;
    }

    @Override
    public int login() throws SQLException {
        List<pet_owner> pet_owners = petOwner.AllGetPetOwner();
        System.out.println("请先登陆");
        System.out.println("请输入宠物主人的名字");
        Scanner scanner = new Scanner(System.in);
        String name= scanner.nextLine();
        System.out.println("请输入此账号的密码");
        String password= scanner.nextLine();
        int flag=0;
        int pid=-1;
        for (pet_owner pet_owner:pet_owners)
        {
            if(pet_owner.getName().equals(name)&&pet_owner.getPassword().equals(password))
            {
                System.out.println("************登陆成功**************");
                System.out.println("************您的基本信息**************");
                System.out.println("名字:"+pet_owner.getName());
                System.out.println("元宝数:"+String.valueOf( pet_owner.getMoney()));
                flag=1;
                pid=pet_owner.getId();
            }
        }
           if (flag==0)
           {
               System.out.println("************账号密码错误，登陆失败**************");
               System.out.println("************请重新输入你的信息**************");
               System.out.println("请输入宠物主人的名字");
               name= scanner.nextLine();
               System.out.println("请输入此账号的密码");
               password= scanner.nextLine();
               for (pet_owner pet_owner:pet_owners)
               {
                   if(pet_owner.getName().equals(name)&&pet_owner.getPassword().equals(password))
                   {
                       System.out.println("************登陆成功**************");
                       System.out.println("************您的基本信息**************");
                       System.out.println("名字:"+pet_owner.getName());
                       System.out.println("元宝数:"+String.valueOf( pet_owner.getMoney()));
                       flag=1;
                       pid=pet_owner.getId();

                   }
               }
           }
          return pid;
        }


}
